package com.woniuxy.mesboot.controller;


import com.woniuxy.mesboot.dto.StorageDTO;
import com.woniuxy.mesboot.entity.Storage;
import com.woniuxy.mesboot.service.StorageService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.vo.StorageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description storage仓库
 * @Date    Create by 2023/5/26 14:03
 */
@RestController
@RequestMapping("/storages")
public class StorageController {
    @Resource
    private StorageService storageService;
    @PostMapping("queryAll")
    public  Result queryAll(@RequestBody StorageDTO dto){
        List<StorageVO> storages = storageService.selectAll(dto);
        return Result.ok().data("list",storages);
    }

    @PostMapping()
    public Result insert(@RequestBody Storage storage) {
        int i = storageService.insertSelective(storage);
        if (i > 0) {
            return Result.ok("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @GetMapping("{id}")
    public Result queryById(@PathVariable Long id) {
        StorageVO storage = storageService.selectInfoById(id);
        return Result.ok().data("vo", storage);
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id) {
        storageService.deleteByPrimaryKey(id);
        int i = storageService.deleteByPrimaryKey(id);
        if (i > 0) {
            return Result.ok("删除成功");

        } else {
            return Result.error("删除失败");
        }


    }

    @PutMapping
    public Result update(@RequestBody Storage orders) {
        int i = storageService.updateByPrimaryKeySelective(orders);
        if (i > 0) {
            return Result.ok("修改成功");

        } else {
            return Result.error("修改失败");
        }
    }
}



